<?php

function html_escape($var)
{
    return htmlspecialchars($var);
}

function sql_protect($var)
{
    //TODO cross SGBD 
    // return mysql_real_escape_string($var);
    $var2 = addslashes($var);
    return $var2;
}

function redirect_to($url)
{
    /* Redirection vers une page différente du même dossier */
    $host  = $_SERVER['HTTP_HOST'];
    $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    header('Location: http://'.$host.$uri.'/'.htmlspecialchars_decode($url));
    exit;
}


function hex_to_rgb($color)
{
    $result['r'] = hexdec(substr($color, 0, 2));
    $result['g'] = hexdec(substr($color, 2, 2));
    $result['b'] = hexdec(substr($color, 4, 2));
    return $result;
}
function separate_name_ext($file)
{
    $result['.ext'] = strrchr($file, '.');
    $result['ext'] = substr($result['.ext'], 1);
    $result['name'] = substr($file, 0, strlen($file) - strlen($result['.ext']));
    return $result;
}

function format_date($date)
{
    if(date('d/m/y', $date) == date('d/m/y', time()) AND date('H', $date) < 6)
        return 'Cette nuit à '.date('H:i', $date);
    elseif(date('d/m/y', $date) == date('d/m/y', time()) AND date('H', $date) >= 6 AND date('H', $date) < 12)
        return 'Ce matin à '.date('H:i', $date);
    elseif(date('d/m/y', $date) == date('d/m/y', time()) AND date('H', $date) >= 12 AND date('H', $date) < 13)
        return 'Ce midi à '.date('H:i', $date);
    elseif(date('d/m/y', $date) == date('d/m/y', time()) AND date('H', $date) >= 13 AND date('H', $date) < 18)
        return 'Cet après-midi à '.date('H:i', $date);
    elseif(date('d/m/y', $date) == date('d/m/y', time()) AND date('H', $date) >= 18)
        return 'Ce soir à '.date('H:i', $date);
//        elseif(date('d/m/y', $date) == date('d/m/y', time()))
//            return 'Aujourd\'hui à '.date('H:i', $date);
    elseif(date('d/m/y', $date) == date('d/m/y', time()-24*3600))
        return 'Hier à '.date('H:i', $date);
    else
        return 'Le '.date('d/m/y à H:i', $date);
}

?>
